package com.yejiali.backend.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yejiali.backend.entity.dto.*;
import com.yejiali.backend.entity.vo.*;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.service.*;
import com.yejiali.backend.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;

/**
 * @Author:yejiali
 * @Date:2025/8/15-08-15-14:20
 * @Decsription:com.yejiali.backend.controller
 * @Version:1.0
 */
@RestController
@RequestMapping("/message")
public class MessageCenterController {
    @Autowired
    private MentionsService mentionsService;

    @Autowired
    private CommentLikeService commentLikeService;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private VideoLikesService videoLikesService;

    @Autowired
    private MessagesService messagesService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 查询消息中心未读消息数
     */
    @GetMapping("/unreadCount/{userId}")
    public Response<UnReadMessageCountVO> unreadCount(@PathVariable Long userId){
        return messagesService.unreadCount(userId);
    }
    /**
     * 标记为已读消息(回复我的)
     */
    @PostMapping("/markReplyMessageAsRead/{userId}")
    public Response<String> markReplyMessageAsRead(@PathVariable Long userId)
    {
        redisUtil.hdel("user:unread:"+userId.toString(),"replyMessage");
        return Response.success("标记成功");
    }
    /**
     * 标记为已读消息(@我的)
     */
    @PostMapping("/markAtMeMessageAsRead/{userId}")
    public Response<String> markAtMeMessageAsRead(@PathVariable Long userId){
        redisUtil.hdel("user:unread:"+userId,"atMeMessage");
        return Response.success("标记成功");
    }
    /**
     * 标记为已读消息(点赞)
     */
    @PostMapping("/markLikeMessageAsRead/{userId}")
    public Response<String> markLikeMessageAsRead(@PathVariable Long userId){
        redisUtil.hdel("user:unread:"+userId,"likeMessage");
        return Response.success("标记成功");
    }
    /**
     *查询给我发消息的人
     */
    @GetMapping("/sendMyMessagePerson/{userId}")
    public Response<?> sendMyMessagePerson(@PathVariable Long userId){
        return messagesService.sendMyMessagePerson(userId);
    }

    /**
     * 查询消息
     */
    @GetMapping("/getMessages")
    public Response<?> getMessages(@RequestParam Long myId,
                                   @RequestParam Long toId){
        return messagesService.getMessages(myId,toId);
    }
    /**
     * 标记私聊消息为已读
     */
    @PostMapping("/markMessageAsRead")
    public Response<String> markMessageAsRead(@RequestBody MessageDTO messageDTO){
        return messagesService.markMessageAsRead(messageDTO.getReceiverId(),messageDTO.getSenderId());
    }
    /**
     * 标记未读消息条数
     */
    @PostMapping("/markUnreadCount")
    public Response<String> markUnreadCount(@RequestBody MessageDTO messageDTO){
        return messagesService.markUnreadCount(messageDTO.getReceiverId(),messageDTO.getSenderId());
    }

    /**
     * @我的
     */
    @GetMapping("/Atme")
    public Response<List<MentionsVO>> getMyMessage(@RequestParam Long userId){
        return mentionsService.getMyMessage(userId);
    }

    /**
     * 回复@我的评论
     */
    @PostMapping("/replyAtMe")
    public Response<String> replyComment(@RequestBody CommentDTO commentDTO) throws JsonProcessingException {
        return mentionsService.replyComment(commentDTO);
    }
    /**
     * 删除@我通知
     */
    @DeleteMapping("/delete")
    public Response<String> deleteMessage(@RequestParam Long mentionId){
        return mentionsService.deleteMessage(mentionId);
    }

    /**
     * 查询回复我的评论的通知
     */
    @GetMapping("/replyComment")
    public Response<CommentReplyDTO> replyComment(@RequestParam Long userId,
                                                  @RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize){
        return commentsService.replyComment(userId,pageNum,pageSize);
    }
    /**
     * 逻辑删除回复我的通知
     */
    @DeleteMapping("/deleteReplyComment")
    public Response<String> deleteReplyComment(@RequestParam Long commentId){
        return commentsService.deleteReplyComment(commentId);
    }
    /**
     * 对回复我的评论的回复
     */
    @PostMapping("/replyReplyComment")
    public Response<String> replyReplyComment(@RequestBody CommentDTO commentDTO){
        return commentsService.replyReplyComment(commentDTO);
    }

    /**
     * 收到的评论点赞
     */
    @GetMapping("/likeComment")
    public Response<List<CommentLikeVO>> receiveLikeComment(@RequestParam Long userId){
        return commentLikeService.receiveLikeComment(userId);
    }
    /**
     * 逻辑删除收到的评论点赞通知
     */
    @PutMapping("/deleteLikeComment/{commentId}")
    public Response<String> deleteLikeComment(@PathVariable Long commentId){
        return commentLikeService.deleteLikeComment(commentId);
    }
    /**
     * 查询收到的视频点赞消息
     */
    @GetMapping("/likeVideo/{userId}")
    public Response<List<VideoLikeVO>> receiveLikeVideo(@PathVariable Long userId){
        return videoLikesService.receiveLikeVideo(userId);
    }
    /**
     * 逻辑删除收到的视频点赞通知
     */
    @PutMapping("/deleteLikeVideo/{videoId}")
    public Response<String> deleteLikeVideo(@PathVariable Long videoId){
        return videoLikesService.deleteLikeVideo(videoId);
    }
}
